package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //只能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart shoppingCarts = shoppingCartMapper.List(shoppingCart);
        if (shoppingCarts != null) { //购物车中已有
            shoppingCarts.setNumber(shoppingCarts.getNumber() + 1);
            shoppingCartMapper.update(shoppingCarts);
        } else {   //购物车中没有，需要新增
            //判断新增套餐还是新增菜品
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null) {   //代表新增的是菜品
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
            } else {                                     //代表新增的是套餐
                Setmeal setmeal = setmealMapper.getSetmealById(setmealId);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车
     * 
     * @return 
     * @author heiheiheng
     * @create 2025/8/8
     **/
    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> shoppingCart = shoppingCartMapper.selectList(BaseContext.getCurrentId());
        return shoppingCart;
    }

    /**
     * 清空购物车
     * 
     * @return 
     * @author heiheiheng
     * @create 2025/8/8
     **/
    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCart);
    }

    /**
     * 删除购物车的一个商品
     * 
 * @param shoppingCartDTO
     * @return 
     * @author heiheiheng
     * @create 2025/8/8
     **/
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart shoppingCarts = shoppingCartMapper.List(shoppingCart);
        Integer number = shoppingCarts.getNumber();
        if (number > 1) {
            shoppingCarts.setNumber(number - 1);
            shoppingCartMapper.update(shoppingCarts);
        }else {
            shoppingCartMapper.delete(shoppingCarts);
        }
    }
}
